package com.hhs.storage.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.hhs.storage.bean.*;
import com.hhs.storage.service.UserService;
import com.hhs.storage.service.YymUserExtService;
import com.hhs.storage.util.QRCodeUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author shadow
 * @since 2018-09-29
 */
@RestController
@RequestMapping("/app/user")
@Slf4j
public class AppUserController {

    @Resource
    UserService userService;
    @Resource
    YymUserExtService yymUserExtService;

    @RequestMapping(value = "getUser")
    public Map<String, Object> getUser(String userid) {
        log.info("查询用户信息");
        AppUserAllInf data = userService.selectAllInf(userid);
        return Result.sucJsonResp(data);
    }
    @RequestMapping(value = "editUser")
    public Map<String, Object> saveOrUpdate(String type,String userid,String value1,String value2){
        log.info("新增或插入用户信息");
        if("1".equals(type)) {
            User user = new User();
            user.setId(userid);
            user.setName(value1);
            userService.saveOrUpdate(user,"");
            return Result.sucJsonResp("保存成功！");
        }else if("2".equals(type)){
            YymUserExt yue=new YymUserExt();
            yue.setUserId(userid);
            yue.setWxNo(value1);
            EntityWrapper<YymUserExt> ew=new EntityWrapper<>();
            ew.eq("user_id_",userid);
            List<YymUserExt> list = yymUserExtService.selectList(ew);
            if(list.size()==1){
                yue.setId(list.get(0).getId());
            }
            yymUserExtService.insertOrUpdate(yue);
            return Result.sucJsonResp("保存成功！");
        }else if("3".equals(type)){
            YymUserExt yue=new YymUserExt();
            yue.setUserId(userid);
            yue.setQq(value1);
            EntityWrapper<YymUserExt> ew=new EntityWrapper<>();
            ew.eq("user_id_",userid);
            List<YymUserExt> list = yymUserExtService.selectList(ew);
            if(list.size()==1){
                yue.setId(list.get(0).getId());
            }
            yymUserExtService.insertOrUpdate(yue);
            return Result.sucJsonResp("保存成功！");
        }else if("4".equals(type)){
            YymUserExt yue=new YymUserExt();
            yue.setUserId(userid);
            yue.setPhoneNo(value1);
            EntityWrapper<YymUserExt> ew=new EntityWrapper<>();
            ew.eq("user_id_",userid);
            List<YymUserExt> list = yymUserExtService.selectList(ew);
            if(list.size()==1){
                yue.setId(list.get(0).getId());
            }
            yymUserExtService.insertOrUpdate(yue);
            return Result.sucJsonResp("保存成功！");
        }else if("5".equals(type)){
            YymUserExt yue=new YymUserExt();
            yue.setUserId(userid);
            yue.setZy(value1);
            EntityWrapper<YymUserExt> ew=new EntityWrapper<>();
            ew.eq("user_id_",userid);
            List<YymUserExt> list = yymUserExtService.selectList(ew);
            if(list.size()==1){
                yue.setId(list.get(0).getId());
            }
            yymUserExtService.insertOrUpdate(yue);
            return Result.sucJsonResp("保存成功！");
        }else if("6".equals(type)){
            User user = new User();
            user.setId(userid);
            user.setPassword(value1);
            userService.saveOrUpdate(user,"");
            return Result.sucJsonResp("保存成功！");
        }
        return Result.failJsonResp("保存错误");
    }
    @RequestMapping(value = "modifypass")
    public Map<String, Object> modifypass(String oldpass,String newpass){
        log.info("修改密码");
        Map<String, Object> mapr=userService.modifypass(oldpass,newpass);
        return mapr;
    }

    @RequestMapping(value = "editRole")
    public Map<String, Object> editRole(String userId,@RequestParam("roleArr[]") String[] roleArr){
        log.info("新增或插入用户信息");
        if(StringUtils.isEmpty(userId)){
            return Result.failJsonResp("账号不能为空");
        }
        return userService.editRole(userId,roleArr);
    }

    @RequestMapping(value = "{id}", method = RequestMethod.DELETE)
    public Map<String, Object> dele(@PathVariable("id") String id) {
        log.info("删除用户");
        userService.dele(id);
        return Result.sucJsonResp(null);
    }

    @RequestMapping(value = "uploadphoto")
    public Map<String, Object> uploadphoto(YymUserExt userExt){
        log.info("更新头像");
        List<YymUserExt> list = yymUserExtService.selectList(new EntityWrapper<YymUserExt>().eq("user_id_", userExt.getUserId()));
        if(list.size()>0){
            YymUserExt saveue=list.get(0);
            saveue.setPhotoPath(userExt.getPhotoPath());
            yymUserExtService.insertOrUpdate(saveue);
        }else{
            yymUserExtService.insertOrUpdate(userExt);
        }
        return Result.sucJsonResp(null);
    }

    @RequestMapping(value = "gettj")
    public Map<String, Object> gettj(User user){
        log.info("得到推荐人列表");
        EntityWrapper<User> ew=new EntityWrapper<>();
        ew.eq("pid_",user.getId());
        List<User> list = userService.selectList(ew);
        return Result.sucJsonResp(list);
    }
}

